<template>
  <div >
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增字段</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['manage:field:edit']"-->
<!--        >修改字段</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['manage:field:remove']"-->
<!--        >删除字段</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出字段</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fieldList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="所属表英文" align="center" prop="id" v-if="false"/>
      <el-table-column label="字段名称" align="center" prop="fieldName" />
      <el-table-column label="字段类型" align="center" prop="fieldType" />
      <el-table-column label="字段长度" align="center" prop="fieldLength" />
      <el-table-column label="字段小数长度" align="center" prop="fieldDecimalLength" />
      <el-table-column label="字段英文" align="center" prop="fieldEn" />
      <el-table-column label="字段定义属性" align="center" prop="fieldBelong" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.fieldBelong == '用户定义字段'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="scope.row.fieldBelong == '用户定义字段'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList(queryParams.tabNameEn)"
    />

    <!-- 添加或修改字段信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属表" prop="tabNameEn">
          <el-input v-model="form.tabNameEn" :disabled="true" />
        </el-form-item>
        <el-form-item label="字段名称" prop="fieldName">
          <el-input v-model="form.fieldName" placeholder="请输入字段名称" />
        </el-form-item>
        <el-form-item label="字段类型" prop="fieldType">
          <el-select v-model="form.fieldType" @change="ftChange" placeholder="请选择字段类型">
            <el-option
              v-for="item in fieldTypeOption"
              :key="item.id"
              :label="item.value"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="字段长度" prop="fieldLength" v-if="flShow">
          <el-input v-model="form.fieldLength" placeholder="请输入字段长度" />
        </el-form-item>

        <el-form-item label="小数长度" prop="fieldDecimalLength" v-if="fdlShow">
          <el-input v-model="form.fieldDecimalLength" placeholder="请输入小数长度" />
        </el-form-item>
        <el-form-item label="字段英文" prop="fieldEn">
          <el-input v-model="form.fieldEn" placeholder="请输入字段英文" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listField, getField, delField, addField, updateField, exportField,getList,haveData } from "@/api/manage/field";

  export default {
    name: "Field",
    components: {
    },
    // props: ["tabNameEn"],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 字段信息表格数据
        fieldList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          tabNameEn: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          fieldEn: [{ required: true, message: "字段英文不能为空", trigger: "blur" }],
          fieldName: [{ required: true, message: "字段名称不能为空", trigger: "blur" }],
          fieldType: [{ required: true, message: "字段类型不能为空", trigger: "blur" }],
          fieldLength: [{ required: true, message: "字段长度不能为空", trigger: "blur" }],
          fieldDecimalLength: [{ required: true, message: "字段小数长度不能为空", trigger: "blur" }],
        },
        fieldTypeOption:[
          {id:'char',value:'固定长度字符'},
          {id:'double',value:'小数类型'},
          {id:'int',value:'整数类型'},
          {id:'varchar',value:'可变长度字符'},
          {id:'datetime',value:'时间类型'}
        ],
        flShow:false,
        fdlShow:false,
      };
    },
    created() {
      // this.getList();
    },
    methods: {

      /** 选择字段类型 */
      ftChange(val){
        if(val == "char" || val == "varchar"){
          this.flShow = true
        }else if(val == "double"){
          this.flShow = true
          this.fdlShow = true
        }else {
          this.flShow = false
          this.fdlShow = false
        }
      },
      /** 查询字段信息列表 */
      getList(tabNameEn) {
        this.loading = true;
        this.queryParams.tabNameEn = tabNameEn
        getList(this.queryParams).then(response => {
          this.fieldList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          fieldName: undefined,
          fieldType: undefined,
          fieldLength: undefined,
          fieldDecimalLength: undefined,
          fieldEn: undefined,
          tabNameEn: undefined,
          delFlag: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.form.tabNameEn = this.queryParams.tabNameEn
        this.open = true;
        this.title = "添加字段信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        haveData(this.queryParams.tabNameEn).then(res => {
          if(res.data > 0){
            this.msgError("该表已有数据不能修改!")
          }else {
            this.reset();
            const id = row.id
            getField(id).then(response => {
              this.form = response.data;
              this.open = true;
              this.title = "修改字段信息";
            });
          }
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateField(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addField(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        haveData(this.queryParams.tabNameEn).then(res => {
          if(res.data > 0){
            this.msgError("该表已有数据不能删除字段!")
          }else {
            const ids = row.id
            this.$confirm('是否确认删除字段信息编号为"' + ids + '"的数据项?', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(function() {
              return delField(ids);
            }).then(() => {
              this.getList();
              this.msgSuccess("删除成功");
            })
          }
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有字段信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportField(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
